﻿package net.wangit.view.element.component;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.support.AbstractElementWriter;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.util.ElementUtil;
import net.wangit.view.enumeration.EnumerationUtil;
import net.wangit.view.util.Html;

public class GlobalRadioFieldElementWriter extends AbstractElementWriter {

	private static final String SUFFIX = ".grf";

	public GlobalRadioFieldElementWriter() {
	}

	protected void writeText(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		MapList enumeration = (MapList) ec.getObject();
		String value = ElementUtil.getValueOrDefault(ac, ec, row);
		Html.set(html, EnumerationUtil.getEnumerationName(enumeration, value));
	}

	protected void writeTextWithValue(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeText(ac, html, ec, row);
		ElementUtil.setHidden(ac, html, ec, row);
	}

	protected void writeControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		html.append(
				"<span onclick=\"$f48(this)\"><input type=\"hidden\" name=\"")
				.append(ElementUtil.getAlias(ec)).append("\"");
		Html.setValue(html, ElementUtil.getValueOrDefault(ac, ec, row));
		html.append("/>");
		writeOptions(ac, html, ec, row, false);
		html.append("</span>");
	}

	protected void writeReadOnlyControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeDisabledControl(ac, html, ec, row);
		ElementUtil.setHidden(ac, html, ec, row);
	}

	protected void writeDisabledControl(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		writeOptions(ac, html, ec, row, true);
	}

	protected void writeHidden(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row) {
		ElementUtil.setHidden(ac, html, ec, row);
	}

	private void writeOptions(ActionContext ac, StringBuffer html,
			ElementComponent ec, Row row, boolean disabled) {
		MapList enumeration = (MapList) ec.getObject();
		if (!Checker.isEmpty(enumeration)) {
			String value = ElementUtil.getValueOrDefault(ac, ec, row);
			String name = (new StringBuilder(String.valueOf(ElementUtil
					.getAlias(ec)))).append(
					GlobalRadioFieldElementWriter.SUFFIX).toString();
			for (int i = 0; i < enumeration.size(); i++) {
				Row item = enumeration.getRow(i);
				String itemValue = item.get("value");
				String itemName = item.get("name");
				String custom = item.get("custom");
				html.append("<input type=\"radio\"");
				if (disabled)
					html.append(" disabled");
				else
					html.append(" name=\"").append(name).append("\"");
				Html.setAttribute(html, custom);
				Html.setAttribute(html, ElementUtil.resolveCustom(ac, ec, row));
				Html.setCss(html, ec.getCss(), "E6");
				Html.setValue(html, itemValue);
				Html.setChecked(html, value, itemValue);
				html.append("/>");
				html.append(itemName);
			}

		}
	}
}
